package com.jwaoo.mobile.db;

import android.content.Context;
import android.view.View;
import com.dx.as.JWCache;
import com.dx.as.buriedpoint.bean.AppInfo;
import com.dx.as.buriedpoint.bean.CrashInfo;
import com.dx.as.buriedpoint.bean.EventInfo;
import com.dx.as.buriedpoint.bean.LogInfo;
import com.dx.as.db.IDBListener;
import com.dx.as.db.SqlLiteHelper;
import com.dx.as.utils.DateUtil;
import com.jwaoo.mobile.common.crash.RunTimeCrashHandler;
import com.jwaoo.mobile.view.explore.bean.UserBean;

import java.util.List;

import http.IHttpListener;

/**
 * Created by jwaoo on 2017/8/12.
 */

public class DBHelper {
    public final static int flg_db = 1000;
    public static void getNearByList(Context context, final IHttpListener httpListener){
        SqlLiteHelper.getInstance(context).dbOption(AppDatabase.class,new IDBListener<AppDatabase>(){
            @Override
            public void onOption(AppDatabase database) {
                List<UserBean> list = database.dbContactsDao().getNearbyList();
                httpListener.onResponse(list,flg_db);
            }
        });
    }

    public static void saveNearByList(Context context){
        SqlLiteHelper.getInstance(context).dbOption(AppDatabase.class,new IDBListener<AppDatabase>(){
            @Override
            public void onOption(AppDatabase database) {
                List<UserBean> list = database.dbContactsDao().getNearbyList();
                database.dbContactsDao().deleteAllNearby(list);
                database.dbContactsDao().insertNearbyList(list);
            }
        });
    }

    public static void saveAppInfo(Context context, final AppInfo appInfo){
        SqlLiteHelper.getInstance(context).dbOption(AppDatabase.class,new IDBListener<AppDatabase>(){
            @Override
            public void onOption(AppDatabase database) {
                database.actionsDao().insertAppInfo(appInfo);
            }
        });
    }

    public static void saveActiveInfo(Context context, IDBListener<AppDatabase> idbListener){
        SqlLiteHelper.getInstance(context).dbOption(AppDatabase.class,idbListener);
    }

    public static void saveCrashInfo(Context context,final Throwable ex){
        SqlLiteHelper.getInstance(context).dbOption(AppDatabase.class,new IDBListener<AppDatabase>(){
            @Override
            public void onOption(AppDatabase database) {
                if (ex != null){
                    String crashBy = ex.toString();
                    String content = RunTimeCrashHandler.getInstance().getStackTrace(ex);
                    long time = DateUtil.getGMTUnixTimeByCalendar();
                    String uuid = JWCache.getAccount();
                    CrashInfo crashInfo = new CrashInfo();
                    crashInfo.setCaseBy(crashBy);
                    crashInfo.setContent(content);
                    crashInfo.setTime(time);
                    crashInfo.setUuid(uuid);
                    database.actionsDao().insertCrashInfo(crashInfo);
                }
            }
        });
    }

    public static void saveOnClickInfo(View v){
        Object object = v.getTag();
        if (object != null){
            final String tag = object.toString();
            SqlLiteHelper.getInstance(v.getContext().getApplicationContext()).dbOption(AppDatabase.class,new IDBListener<AppDatabase>(){
                @Override
                public void onOption(AppDatabase database) {
                    EventInfo eventInfo = new EventInfo();
                    eventInfo.setUuid(JWCache.getAccount());
                    eventInfo.setHappen_time(DateUtil.getGMTUnixTimeByCalendar());
                    eventInfo.setTag(tag);
                    database.actionsDao().insertEventInfo(eventInfo);
                }
            });
        }
    }

    public static void saveLogInfo(Context context, final String tag,final String content){
            SqlLiteHelper.getInstance(context).dbOption(AppDatabase.class,new IDBListener<AppDatabase>() {
                @Override
                public void onOption(AppDatabase database) {
                    LogInfo logInfo = new LogInfo();
                    logInfo.setTag(tag);
                    logInfo.setContent(content);
                    long time = DateUtil.getGMTUnixTimeByCalendar();
                    logInfo.setTime(time);
                    database.actionsDao().insertLogInfo(logInfo);
                }
            });
    }
}
